import { getOssToken } from '@/apis/assignment/index'
import OSS from 'ali-oss'
import { ElMessage } from 'element-plus'

import excelImg from '@/assets/images/common/excel.png'
import wordImg from '@/assets/images/common/word.png'
import pdfImg from '@/assets/images/common/pdf.png'
import pptImg from '@/assets/images/common/ppt.png'
import txtImg from '@/assets/images/common/txt.png'
import otherImg from '@/assets/images/common/other.png'

const getToken = async () => {
  let localToken = localStorage.getItem('OSS-TOKEN') || '{}'
  localToken = JSON.parse(localToken)
  if (
    !localToken.Expiration ||
    localToken.Expiration ||
    Date.now() / 1000 > localToken.Expiration
  ) {
    const res = await getOssToken()
    localToken = res.data
    localStorage.setItem('OSS-TOKEN', JSON.stringify(localToken))
  }
  return localToken
}
export const uploadImg = async file => {
  const tokenData = await getToken()
  let ossConfig = {
    region: tokenData.region,
    accessKeyId: tokenData.AccessKeyId,
    accessKeySecret: tokenData.AccessKeySecret,
    stsToken: tokenData.SecurityToken,
    bucket: tokenData.bucket,
    secure: true
  }
  const client = new OSS(ossConfig)
  const Filepath = '/ailearn/teacher/' + file.name
  const headers = {
    'x-oss-object-acl': 'public-read'
  }
  const result = await client.put(Filepath, file, { headers })
  return result
}

export const uploadFile = async file => {
  const tokenData = await getToken()
  let ossConfig = {
    region: tokenData.region,
    accessKeyId: tokenData.AccessKeyId,
    accessKeySecret: tokenData.AccessKeySecret,
    stsToken: tokenData.SecurityToken,
    bucket: tokenData.bucket,
    secure: true
  }
  let client = new OSS(ossConfig)
  let Filepath = '/ailearn/teacher/' + file.name
  const raw = file.raw
  let FileData = new File([raw], file.name, {
    type: raw.type
  })
  let headers = {
    'x-oss-object-acl': 'public-read'
  }

  let result = null

  // 根据文件大小设置上传方式
  result = await client.put(Filepath, FileData, { headers })

  return {
    name: result.name.slice(16),
    url: result.url,
    type: file.raw.type
  }
}

export function matchFileSuffixType(fileName) {
  // 后缀获取
  let suffix = ''
  // 获取类型结果
  let result = ''
  try {
    let flieArr = fileName.split('.')
    suffix = flieArr[flieArr.length - 1]
  } catch (err) {
    suffix = ''
  }

  // 匹配txt
  let txtlist = ['txt']
  result = txtlist.some(function (item) {
    return item == suffix
  })
  if (result) {
    result = 'txt'
    return result
  }

  // 匹配 excel
  let excelist = ['xls', 'xlsx']
  result = excelist.some(function (item) {
    return item == suffix
  })
  if (result) {
    result = 'excel'
    return result
  }

  // 匹配 word
  let wordlist = ['doc', 'docx']
  result = wordlist.some(function (item) {
    return item == suffix
  })
  if (result) {
    result = 'word'
    return result
  }

  // 匹配 pdf
  let pdflist = ['pdf']
  result = pdflist.some(function (item) {
    return item == suffix
  })
  if (result) {
    result = 'pdf'
    return result
  }

  // 匹配 ppt
  let pptlist = ['ppt']
  result = pptlist.some(function (item) {
    return item == suffix
  })
  if (result) {
    result = 'ppt'
    return result
  }

  // 其他 文件类型
  result = 'other'
  return result
}

/**
 * 获取文件对应的图片
 */
export const getFileImg = fileName => {
  let img = matchFileSuffixType(fileName)
  switch (img) {
    case 'excel':
      img = excelImg
      break
    case 'word':
      img = wordImg
      break
    case 'pdf':
      img = pdfImg
      break
    case 'ppt':
      img = pptImg
      break
    case 'txt':
      img = txtImg
      break
    default:
      img = otherImg
  }
  return img
}

/**
 * 限制文件大小
 */
export const beforeUpload = rawFile => {
  if (rawFile.size / 1024 / 1024 > 100) {
    ElMessage.error('上传文件需小于100M')
    return false
  }
  return true
}
